<template>
  <div class="vipView">
    <!-- 头部 -->
    <header v-if="user">
      <div class="user">
        <router-link tag="div" to="/my" class="logo">
          <img :src="user.userImg" alt="" />
        </router-link>
        <div class="text">
          <div class="name">{{ user.username }}</div>
          <div class="desc">
            <img src="../assets/imgs/viplogo.png" alt="" />
            {{ user.isVip ? "尊贵的黄金读书会员" : "开通会员，海量小说免费读" }}
          </div>
        </div>
      </div>
      <div class="open_vip">
        <div class="left">
          {{ user.isVip ? "尊贵的会员，您享有以下权益" : "尚未开通会员" }}
        </div>
        <div class="right">
          <div class="text" @click="popup_show = true" v-show="!user.isVip">
            立即开通
          </div>
          <div class="btn" v-show="!user.isVip"></div>
          <img v-show="user.isVip" src="../assets/imgs/viplogo.png" alt="" />
        </div>
      </div>
    </header>
    <!-- 头部 -->

    <!-- 会员权益 -->
    <div class="vip_power">
      <div class="title">
        <h3>会员权益</h3>
        <div class="right">全部特权</div>
      </div>
      <div class="list">
        <div class="item">
          <div class="img">
            <img src="../assets/imgs/Power_1.png" alt="" />
          </div>
          <div class="text">包月书库</div>
        </div>
        <div class="item">
          <div class="img">
            <img src="../assets/imgs/Power_2.png" alt="" />
          </div>
          <div class="text">尊贵身份</div>
        </div>
        <div class="item">
          <div class="img">
            <img src="../assets/imgs/Power_3.png" alt="" />
          </div>
          <div class="text">购买折扣</div>
        </div>
        <div class="item">
          <div class="img">
            <img src="../assets/imgs/Power_4.png" alt="" />
          </div>
          <div class="text">超级特权</div>
        </div>
      </div>
    </div>
    <!-- 会员权益 -->

    <!-- 推荐列表 -->
    <div class="list3 l_type2 wrap">
      <div class="title">
        <h3>猜你喜欢</h3>
        <p @click="bookSort(mbLike)">换一换</p>
      </div>

      <div class="content">
        <router-link
          :to="'/details?id=' + vl.bookId"
          v-for="(vl, i) in mbLike"
          :key="vl.bookId"
          v-show="i < 6"
          class="item"
        >
          <div class="img">
            <img :src="vl.imgUrl" alt="" />
          </div>
          <div class="text">
            <div class="title">{{ vl.bookName }}</div>
            <p>{{ vl.desc }}</p>
            <div class="author">
              <div class="name">{{ vl.authorName }}</div>
              <div class="type">废柴</div>
            </div>
            <div class="line"></div>
          </div>
        </router-link>
      </div>
    </div>
    <!-- 推荐列表 -->

    <!-- todo banner -->
    <div class="footer-info wrap">
      <span class="text">阿里文学出品</span>
      <div class="line"></div>
    </div>
    <!-- todo banner -->

    <van-popup
      class="popup1"
      v-model="popup_show"
      :style="{ height: '30%', width: '70%' }"
    >
      <div class="is_open_vip">
        <div class="box box1" v-show="!isPay">
          <div class="question">
            <p>是否花费<span>300书豆</span></p>
            <p>成为尊贵的VIP会员?</p>
          </div>
          <div class="btn">
            <div class="no" @click="popup_show = false">否</div>
            <div class="yes" @click="pay">是</div>
          </div>
        </div>
        <div class="box box2" v-show="isPay">
          <div class="question">
            <p>书豆余额不足</p>
            <p>是否前往充值页面</p>
          </div>
          <div class="btn">
            <div class="no" @click="popup_show = false">否</div>
            <router-link tag="div" to="/recharge" class="yes">是</router-link>
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import { getGirlClassify } from "../api/classify";
export default {
  data() {
    return {
      user: null, // 当前用户数据
      userInfo: null, //所有用户数据
      popup_show: false, // 弹出层显示隐藏
      isPay: false, // 用户购买意愿
      mbLike: null, // 猜你喜欢列表
    };
  },
  methods: {
    getUserDataFun() {
      let userId = localStorage.getItem("userId");
      this.userInfo = JSON.parse(localStorage.getItem("userInfo"));
      this.user = this.userInfo[userId];
      console.log(this.user);
    },
    // 购买会员
    pay() {
      let userId = localStorage.getItem("userId");
      let balance = this.user.balance;
      if (balance >= 300) {
        this.userInfo[userId].balance -= 300;
        this.userInfo[userId].isVip = true;
        localStorage.setItem("userInfo", JSON.stringify(this.userInfo));

        this.popup_show = false;

        this.$toast({
          message: "开通成功",
          position: "bottom",
        });

        this.getUserDataFun();
      } else {
        this.isPay = true;
      }
    },
    // 打乱列表顺序
    bookSort(array) {
      array.sort(() => (Math.random() > 0.5 ? 1 : -1));
    },
  },
  created() {
    this.getUserDataFun();
    getGirlClassify().then((data) => {
      console.log(data.modulesInfos[1].data.books);
      this.mbLike = data.modulesInfos[1].data.books;
    });
  },
};
</script>

<style lang="scss" scoped>
.vipView {
  > header {
    padding: 16px 16px 0;
    background: linear-gradient(#fff 60%, #e6e6e6);

    > .user {
      display: flex;

      > .logo {
        width: 60px;
        height: 60px;
        margin-right: 15px;

        > img {
          width: 100%;
          border-radius: 50%;
        }
      }

      > .text {
        > .name {
          height: 30px;
          line-height: 30px;
          font-size: 14px;
        }

        > .desc {
          display: flex;
          font-size: 12px;
          color: #cca985;

          > img {
            width: 17px;
            height: 15px;
            margin-right: 4px;
          }
        }
      }
    }

    > .open_vip {
      height: 52px;
      padding: 0 15px;
      margin-top: 24px;
      background: url(../assets/imgs/vip-none.png) no-repeat;
      background-size: contain;
      display: flex;
      justify-content: space-between;
      align-items: center;
      color: #fff;
      text-shadow: 0 2px 4px rgb(0 0 0 / 10%);

      > .left {
        font-size: 17px;
        font-weight: bold;
      }

      > .right {
        display: flex;
        align-items: center;
        font-size: 13px;
        line-height: 13px;

        > .btn {
          width: 8px;
          height: 12px;
          background: url(http://www.hlamall.cn/img/mobile-site/gobal/arrow-white.png)
            no-repeat;
          background-size: contain;
          margin-left: 4px;
        }

        > img {
          height: 30px;
        }
      }
    }
  }

  > .vip_power {
    padding: 30px 15px 10px;

    > .title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 22px;
      line-height: 22px;
      padding-bottom: 24px;

      > h3 {
        font-size: 18px;
      }

      > .right {
        font-weight: bold;
        font-size: 14px;
        color: #cca985;
      }
    }

    > .list {
      display: flex;

      > .item {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        justify-content: center;

        > .img {
          width: 48px;
          height: 48px;
          margin-bottom: 10px;

          > img {
            width: 100%;
          }
        }

        .text {
          font-size: 12px;
          line-height: 16px;
          color: #999;
        }
      }
    }
  }

  .l_type2 {
    box-sizing: border-box;
    padding: 8px 16px 0;

    > .title {
      height: 20px;
      line-height: 20px;
      padding: 24px 0 12px;
      font-size: 18px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      > p {
        color: #cca985;
        font-size: 14px;
      }
    }

    > .content {
      > .item {
        display: flex;
        width: 100%;

        &:last-of-type > .text .line {
          height: 0;
        }

        > .img {
          width: 32%;
          margin: 8px 0;

          > img {
            width: 100%;
            height: 100%;
          }
        }

        > .text {
          flex: 1;
          display: flex;
          flex-wrap: wrap;
          margin-top: 16px;
          margin-left: 12px;
          position: relative;
          flex-direction: column;
          justify-content: space-between;

          > .title {
            color: #333;
            font-size: 16px;
          }

          > p {
            height: 40px;
            line-height: 20px;
            margin: 15px 0;
            font-size: 13px;
            color: #999;
            overflow: hidden;
            text-overflow: ellipsis;
          }

          > .author {
            display: flex;
            width: 100%;
            justify-content: space-between;
            padding: 20px 0 12px;
            line-height: 24px;
            color: #999;
            font-size: 13px;

            > .type {
              width: 24px;
              height: 12px;
              line-height: 12px;
              padding: 4px 8px;
              background: #f5f5f5;
              border-radius: 4px;
              color: #000;
              font-size: 12px;
            }
          }

          > .line {
            position: absolute;
            bottom: 0;
            width: 100%;
            height: 1px;
            background: #e5e5e5;
            transform: scaleY(0.5);
          }
        }
      }
    }
  }

  > .popup1 {
    border: 1px solid #ccc;
    border-radius: 5px;

    .is_open_vip {
      height: 99%;

      > .box {
        width: 100%;
        height: 100%;

        > .question {
          height: 75%;
          display: flex;
          flex-wrap: wrap;
          justify-content: center;
          align-content: center;
          border-bottom: 1px solid #ccc;

          > p {
            height: 30px;
            width: 100%;
            text-align: center;
            line-height: 30px;
            font-size: 18px;

            > span {
              color: #23b383;
              font-weight: bold;
            }
          }
        }

        > .btn {
          display: flex;
          height: 25%;

          > div {
            flex: 1;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 13px;
            color: #23b383;
            font-weight: bold;

            &:nth-of-type(1) {
              color: #333;
              border-right: 1px solid #ccc;
            }
          }
        }
      }
    }
  }

  .footer-info {
    height: 18px;
    line-height: 18px;
    color: #ccc;
    font-size: 13px;
    text-align: center;
    padding: 40px 0px;
    position: relative;

    > .text {
      padding: 0 10px;
      background: #fff;
      position: relative;
      z-index: 10;
    }

    > .line {
      position: absolute;
      left: 0;
      right: 0;
      top: 9px;
      bottom: 10px;
      margin: auto;
      height: 1px;
      width: 50%;
      background: #ccc;
    }
  }
}
</style>